package com.ghy.web.backend;

import com.ghy.entity.EbUser;
import com.ghy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/list")
    public String list(Model entity,
                       @RequestParam(value = "page", defaultValue = "1") int psize,
                       @RequestParam(value = "pageSize", defaultValue = "3") int pageSize) {
        System.out.println("start service.....;psize==>" + psize);
        List<EbUser> userList = userService.listByPage(psize, pageSize);
        int totalPage = userService.getMaxPage(pageSize);
        entity.addAttribute("userList", userList);
        entity.addAttribute("totalPage", totalPage);
        entity.addAttribute("curPage", psize);
        return "manage/user";
    }

    @RequestMapping("/to_save")
    public String toUserAdd() {

        return "manage/user-add";
    }

    @RequestMapping("/save")
    public String save(EbUser ebUser,@RequestParam("A") Integer A,@RequestParam("B") Integer B,@RequestParam("C") Integer C)throws Exception {
        System.out.println("ebUser==>"+ebUser);
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        String sr = A+"-"+B+"-"+C;
        ebUser.setEuBirthday(format1.parse(sr));
        ebUser.setEuStatus(1);
        System.out.println(ebUser);
        Integer result = userService.save(ebUser);
        if (result<1){
            return "manage/manage-error";
        }
        return  "manage/manage-result";
    }
    @RequestMapping("/to_edit")
    public String toEdit(Model entity,@RequestParam("euUserId")String euUserId){
        System.out.println("点击修改跳过来了,euUserId="+euUserId);
        EbUser ebUser = userService.selectOne(euUserId);
        entity.addAttribute("ebUser",ebUser);
        return "manage/user-modify";
    }
    @RequestMapping("/edit")
    public String edit(Model entity,String euUserId){
        EbUser ebUser = userService.selectByCondition(euUserId);
       entity.addAttribute("ebUser",ebUser);
        return "manage/user-modify";
    }
    @RequestMapping("/editSubmit")
    public String editSubmit(EbUser ebUser){
        System.out.println(ebUser);
        int result = userService.updateUser(ebUser);
        if (result <= 0){
            return "manage/error";
        }
        return "manage/manage-result";
    }
    @RequestMapping("/{id}")
    public String delete(@PathVariable("id") String id){
        int result=userService.delete(id);
        if(result==0){
            return "manage/error";
        }
        return "manage/manage-result";
    }
}